<!-- 饼状图 https://echarts.apache.org/examples/zh/editor.html?c=pie-borderRadius -->

<template>
  <div class="chart-container">
    <div ref="chart" class="chart"></div>
  </div>
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    list: {
      type: Array,
      required: true,
    },
    title: {
      type: String,
      required: true,
    },
  },
  mounted() {
    this.renderChart();
  },
  watch: {
    list: {
      handler() {
        this.renderChart();
      },
      deep: true,
    },
  },
  methods: {
    renderChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom, "");
      const option = this.generateOption();
      myChart.setOption(option);
      window.addEventListener("resize", () => {
        myChart.resize();
      });
    },
    generateOption() {
      const data = this.list.map((item) => {
        return {
          value: item.total,
          name: item.name,
        };
      });

      const option = {
        title: {
          text: this.title,
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "司机人数",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: data,
          },
        ],
      };

      return option;
    },
  },
};
</script>
<style lang="scss" scoped>

</style>
